/**
 * 
 */
package app.tuwien.isi.tweetanalyser;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author Philipp Pospisil
 *
 */
public class StatisticActivity extends Activity{
	
	private ArrayList<Tweet> tweets;
	private List<Statistic> statistics;
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic);
	    
		Intent intent = getIntent();
		
		if(intent.getParcelableArrayExtra("tweets") != null){
			Log.e("TweetList","is null at Statistic Activity");
		}else{
			tweets = intent.getParcelableArrayListExtra("tweets");
			AsyncTask<Uri, Object, List<Statistic>> task = new TwitterTask();
			
	        task.execute();
			
			listView = (ListView) findViewById(R.id.statisticList);
			
			listView.setOnItemClickListener(new OnItemClickListener() {
	            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
	            	if(statistics.get(position).getCategory().equals("Language")){
	            		Intent intent = new Intent (getApplicationContext(), SmallStatisticActivity.class);
	                	
	                	if(statistics.get(position).getLengthOfList() == 0){
	                		Log.e("Language","is null");
	                	}else{
	                		int size = statistics.get(position).getLengthOfList();
	                		String[] language = new String[size];
	                		int[] sum = new int[size];
	                		for(int i=0;i<size;i++){
	                			language[i] = statistics.get(position).getItemOfList(i);
	                			sum[i] = statistics.get(position).getSumOfItem(i);
	                		}
	    	            	intent.putExtra("item", language);
	    	            	intent.putExtra("sum", sum);
	    	            	startActivityForResult(intent, 1);
	                	}
	            	}else{
	            		if(statistics.get(position).getCategory().equals("Places")){
		            		Intent intent = new Intent (getApplicationContext(), StatisticDetailsActivity.class);
		            		
		            		if(statistics.get(position).getLengthOfList() == 0){
		                		Log.e("Place","is null");
		                	}else{
		                		int size = statistics.get(position).getLengthOfList();
		                		String[] place = new String[size];
		                		int[] sum = new int[size];
		                		String[] type = new String[size];
		                		String[] country = new String[size];
		                		String[] coordinates = new String[size];
		                		
		                		for(int i=0;i<size;i++){
		                			place[i] = statistics.get(position).getItemOfList(i);
		                			sum[i] = statistics.get(position).getSumOfItem(i);
		                			type[i] = statistics.get(position).getTypOfItem(i);
		                			country[i] = statistics.get(position).getCountryOfItem(i);
		                			coordinates[i] = statistics.get(position).getCoordinatesOfItem(i);
		                		}
		                		intent.putExtra("statistic", "place");
		    	            	intent.putExtra("place", place);
		    	            	intent.putExtra("sum", sum);
		    	            	intent.putExtra("type", type);
		    	            	intent.putExtra("country", country);
		    	            	intent.putExtra("coordinates", coordinates);
		    	            	startActivityForResult(intent, 1);
		                	}
		            	}else{
		            		if(statistics.get(position).getCategory().equals("User")){
			            		Intent intent = new Intent (getApplicationContext(), StatisticDetailsActivity.class);
			            		
			            		if(statistics.get(position).getLengthOfList() == 0){
			                		Log.e("User","is null");
			                	}else{
			                		int size = statistics.get(position).getLengthOfList();
			                		String[] user = new String[size];
			                		int[] sum = new int[size];
			                		String[] location = new String[size];
			                		String[] language = new String[size];
			                		int[] followers = new int[size];
			                		
			                		for(int i=0;i<size;i++){
			                			user[i] = statistics.get(position).getItemOfList(i);
			                			sum[i] = statistics.get(position).getSumOfItem(i);
			                			location[i] = statistics.get(position).getLocationOfItem(i);
			                			language[i] = statistics.get(position).getLanguageOfItem(i);
			                			followers[i] = statistics.get(position).getFollowersOfItem(i);
			                		}
			                		intent.putExtra("statistic", "user");
			    	            	intent.putExtra("user", user);
			    	            	intent.putExtra("sum", sum);
			    	            	intent.putExtra("location", location);
			    	            	intent.putExtra("language", language);
			    	            	intent.putExtra("followers", followers);
			    	            	startActivityForResult(intent, 1);
			                	}
			            	}else{
			            		if(statistics.get(position).getCategory().equals("Countries")){
				            		Intent intent = new Intent (getApplicationContext(), SmallStatisticActivity.class);
				                	
				                	if(statistics.get(position).getLengthOfList() == 0){
				                		Log.e("Countries","is null");
				                	}else{
				                		int size = statistics.get(position).getLengthOfList();
				                		String[] countries = new String[size];
				                		int[] sum = new int[size];
				                		for(int i=0;i<size;i++){
				                			countries[i] = statistics.get(position).getItemOfList(i);
				                			sum[i] = statistics.get(position).getSumOfItem(i);
				                		}
				    	            	intent.putExtra("item", countries);
				    	            	intent.putExtra("sum", sum);
				    	            	startActivityForResult(intent, 1);
				                	}
				            	}else{
				            		if(statistics.get(position).getCategory().equals("Hashtag")){
					            		Intent intent = new Intent (getApplicationContext(), SmallStatisticActivity.class);
					                	
					                	if(statistics.get(position).getLengthOfList() == 0){
					                		Log.e("Hashtag","is null");
					                	}else{
					                		int size = statistics.get(position).getLengthOfList();
					                		String[] hashtag = new String[size];
					                		int[] sum = new int[size];
					                		for(int i=0;i<size;i++){
					                			hashtag[i] = statistics.get(position).getItemOfList(i);
					                			sum[i] = statistics.get(position).getSumOfItem(i);
					                		}
					                		intent.putExtra("query", "notnull ;)");
					    	            	intent.putExtra("item", hashtag);
					    	            	intent.putExtra("sum", sum);
					    	            	startActivityForResult(intent, 1);
					                	}
					            	}
				            	}
			            	}
		            	}
	            	}
	            }
	        });
		}
	 }
	
    
    public List<Statistic> getStatistics() {
		return statistics;
	}


	public void setStatistics(List<Statistic> statistics) {
		this.statistics = statistics;
	}


	private class TwitterTask extends AsyncTask<Uri, Object, List<Statistic>> {
    	
        public TwitterTask() {
		}
        
		@Override
		protected void onPostExecute(List<Statistic> result) {
			statistics = result;
			listView.setAdapter(new StatisticAdapter(StatisticActivity.this, result));
        }
		
		@Override
		protected List<Statistic> doInBackground(Uri... arg0) {
			List<Statistic> listStatistics = new ArrayList<Statistic>();
			
			Statistic place = new Statistic();
			place.setCategory("Places");
			
			Statistic countries = new Statistic();
			countries.setCategory("Countries");
			
			Statistic user = new Statistic();
			user.setCategory("User");		
			
			Statistic lang = new Statistic();
			lang.setCategory("Language");
			
			Statistic hashtags = new Statistic();
			hashtags.setCategory("Hashtag");
			
			for(int i=0; i<tweets.size();i++){
				if(tweets.get(i).getPlace() != null){
					place.addPlaceItemToList(tweets.get(i).getPlace(), tweets.get(i).getPlaceType(), 
							tweets.get(i).getCountry(), tweets.get(i).getCoordinates());
				}
				if(tweets.get(i).getCountry() != null){
					countries.addItemToList(tweets.get(i).getCountry());
				}
				if(tweets.get(i).getUserName() != null){
					user.addUserItemToList(tweets.get(i).getUserName(), tweets.get(i).getUserLocation(), 
							tweets.get(i).getLanguage(), tweets.get(i).getFollowers());
				}
				if(tweets.get(i).getLanguage() != null){
					lang.addItemToList(tweets.get(i).getLanguage());
				}
				if(tweets.get(i).getText() != null){
					String text = tweets.get(i).getText();
					String hashtag;
					int index = text.indexOf("#");
					
					while (index != -1){
						text = text.substring(index+1);
						if(text.indexOf(" ") != -1){
							hashtag = text.substring(0, text.indexOf(" "));
						}else{
							hashtag = text;
						}
						hashtags.addItemToList(hashtag);
						index = text.indexOf("#");
					}
				}
			}
			
			listStatistics.add(place);
			listStatistics.add(countries);
			listStatistics.add(user);
			listStatistics.add(lang);
			listStatistics.add(hashtags);
			
            return listStatistics;
        }
    }
}
